package org.stvd.core.util.convert;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.stvd.core.process.ErrorInputStreamWatchThread;
import org.stvd.core.process.InputStreamWatchThread;
import org.stvd.core.util.LogUtil;

public class Pdf2Swf extends Thread {

    private final String sourcePath;
    private final String destPath;
    private final String fileName;

    public Pdf2Swf(String sourcePath, String destPath, String fileName) {
        this.sourcePath = sourcePath;
        this.destPath = destPath;
        this.fileName = fileName;
    }

    public void run() {
        try {
            convert(sourcePath, destPath, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean convert(String sourcePath, String destPath, String fileName) throws IOException {
        // 目标路径不存在则建立目标路径
        File dest = new File(destPath);
        if (!dest.exists())
            dest.mkdirs();

        // 源文件不存在则返回
        File source = new File(sourcePath);
        if (!source.exists()) {
            LogUtil.info("pdf转换swf失败：文件" + sourcePath + "不存在！");
            return false;
        }

        // 因为下面进行系统调用，这样就会把系统执行的操作新开启一个线程（在此linux也叫进程），所以它和主扫描程序是独立运行，所以下次还会扫描这个转换中的文件，所以这里要将它设置为不可读，
        source.setReadable(false);

        List<String> command = new ArrayList<String>();
        // 将pdf2swf文件拷贝到system32目录下，就可以直接使用命令进行转换了
        command.add("d:\\pdf2swf.exe");
        command.add("-z");
        // command.add("-B");
        // command.add("D:\\opensource\\SWFTools\\rfxview.swf");
        command.add("-s");
        command.add("flashversion=9");
        command.add("-s");
        command.add("poly2bitmap");
        // command.add("languagedir=C:/xpdf/chinese-simplified/");
        command.add(sourcePath);
        command.add("-o");
        command.add(destPath + "\\" + fileName);

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(command);
        Process process = processBuilder.start();

        InputStreamWatchThread inputStreamWatchThread = new InputStreamWatchThread(process);
        inputStreamWatchThread.start();

        ErrorInputStreamWatchThread errorInputStreamWatchThread = new ErrorInputStreamWatchThread(process);
        errorInputStreamWatchThread.start();

        try {
            process.waitFor();
            inputStreamWatchThread.setOver(true);
            errorInputStreamWatchThread.setOver(true);
        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
            return false;
        }

        LogUtil.info("转换完成！");

        return true;
    }

//    public static void main(String[] args) throws IOException {
        // Converter.pdf2swf("D:\\FileData\\ABC\\20130427-admin-0000067.pdf",
        // "D:\\FileData\\ABC\\", "20130427-admin-0000067.swf");
        // Converter.pdf2swf("D:\\FileData\\ABC\\20130427-admin-0000068.pdf",
        // "D:\\FileData\\ABC\\", "20130427-admin-0000068.swf");
        // converter.convertPDF2SWF("D:\\FileData\\I13-10533-111367.pdf",
        // "D:\\FileData\\123456\\", "I13-10533-111367.swf");
//        Pdf2Swf pdf2swf = new Pdf2Swf("D:\\FileData\\ABC\\20130427-admin-0000067.pdf", "D:\\FileData\\ABC\\",
//                "20130427-admin-0000067.swf");
//        pdf2swf.start();
//    }
}